package gui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wb.swt.SWTResourceManager;

public class Loader extends Dialog {

	protected Object result;
	protected Shell shlLoad;
	private String title;
	private int backgroundColor;
	private Image image;
	private int border;
	private int imageWidth;
	private long loadTime;
	
	private ProgressBar progressBar;
	private Canvas canvas;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public Loader(Shell parent, int style, String title, int backgroundColor, Image image, int border, int imageWidth, long loadTime) {
		super(parent, style);
		this.title = title;
		this.backgroundColor = backgroundColor;
		this.image = image;
		this.border = border;
		this.imageWidth = imageWidth;
		this.loadTime = loadTime;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlLoad.open();
		shlLoad.layout();
		Display display = getParent().getDisplay();
		quasiLoad();
		while (!shlLoad.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlLoad = new Shell(getParent(), getStyle());
		shlLoad.setSize(450, 325);
		shlLoad.setText(this.title);
		
		canvas = new Canvas(shlLoad, SWT.NONE);
		canvas.setBackground(SWTResourceManager.getColor(backgroundColor));
		if((Integer) this.imageWidth != null) {
			int canvasHeight =  (int) (((double) this.imageWidth / (double) this.image.getBounds().width) * this.image.getBounds().height);
			canvas.setBounds(this.border, this.border, this.imageWidth, canvasHeight); 
		} else { 
			canvas.setBounds(10, 10, 428, 200);
		}
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent pe) {
				GC gc = new GC(canvas);
				//Image image = SWTResourceManager.getImage(WorldSettings.class, "/resources/GrowthOfABoidTransparent.png");
				gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height,
						0, 0, canvas.getBounds().width, canvas.getBounds().height);
			}
		});
		
		progressBar = new ProgressBar(shlLoad, SWT.NONE);
		progressBar.setBounds(border, canvas.getBounds().y + canvas.getBounds().height + border, canvas.getBounds().width, 20);
		
		org.eclipse.swt.graphics.Point shlOptimalSize = shlLoad.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		shlLoad.setSize(shlOptimalSize.x + border, shlOptimalSize.y + border);
	}
	
	/**
	 * Gives the appearance of loading by incrementing a progress bar; in reality it just sleeps and then terminates.
	 */
	public void quasiLoad() {
		canvas.redraw(); //this assures that the image is displayed while loading
		long sleepIncrement = this.loadTime / 100;
		for(int i = 0; i < 100; i++) {
			progressBar.setSelection(progressBar.getSelection() + 1);
			try { Thread.sleep(sleepIncrement);
			} catch (InterruptedException ie) {
				ie.printStackTrace();
			}
		}
		shlLoad.close();
	}
}
